<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nyext="urn:noury-extensions">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="outdir" />
  <xsl:param name="srcdir" />
  <xsl:param name="tmpdir" />
  <xsl:param name="trmdir" />

  <xsl:include href="functions.xslt" />

	<msxsl:script language="C#" implements-prefix="nyext">
		<![CDATA[
      public static void createDirectory(string basedir, string path) {
        System.IO.Directory.CreateDirectory(
          System.IO.Path.Combine(basedir, path)
        );
      }

      public static void startTransform(string path, string args) {
        System.Console.Out.WriteLine(@"..\.NET\Tools\XslTransform\XslTransform.exe "+args);
        using (
          System.Diagnostics.Process p=System.Diagnostics.Process.Start(
            System.IO.Path.Combine(path, @"Tools\XslTransform\XslTransform.exe"),
            args
          )
        ) {
          p.WaitForExit(1000);
        }
      }

      public static void startProcess(string path, string command, string args) {
        System.Console.Out.WriteLine(path+@"\"+command+" "+args);
        using (
          System.Diagnostics.Process p=System.Diagnostics.Process.Start(
            System.IO.Path.Combine(path, command),
            args
          )
        ) {
//          p.WaitForExit();
        }
      }

		]]>
	</msxsl:script>

  <xsl:template match="directory">
    <xsl:variable name="path">
      <xsl:apply-templates select="." mode="path-from-root">
        <xsl:with-param name="root" select="ancestor::manifest[1]" />
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:value-of select="nyext:createDirectory(nyext:combinePath($outdir, $path), @name)" />
	  <xsl:apply-templates select="*" />
  </xsl:template>
  
  <xsl:template match="execution" mode="generation">
    <xsl:param name="output" />
    <xsl:variable name="path">
      <xsl:apply-templates select="." mode="path-from-root">
        <xsl:with-param name="root" select="ancestor::manifest[1]" />
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="args" select="nyext:stringFormat(@args, nyext:combinePath($trmdir, @input), nyext:combinePath($outdir, nyext:combinePath($path, $output)))" />
    <xsl:value-of select="nyext:startProcess(nyext:combinePath($trmdir, 'Tools'), @command, $args)" />
  </xsl:template>

  <xsl:template match="transformation" mode="generation">
    <xsl:param name="params" />
    <xsl:param name="output" />
    <xsl:variable name="p">
      <xsl:value-of select="$params" />
      <xsl:if test="$params and count(param) &gt; 0">
        <xsl:text>,</xsl:text>
      </xsl:if>
      <xsl:for-each select="param">
        <xsl:value-of select="@name" />
        <xsl:text>=</xsl:text>
        <xsl:value-of select="@value" />
        <xsl:if test="position()!=last()">
          <xsl:text>,</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="args">
      <xsl:text>/xsl:"</xsl:text>
        <xsl:value-of select="nyext:getShortPathName(nyext:combinePath($trmdir, @transform))" />
      <xsl:text>" "</xsl:text>
      <xsl:choose>
        <xsl:when test="@input">
          <xsl:value-of select="nyext:getShortPathName(nyext:combinePath($srcdir, @input))" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="nyext:getShortPathName(nyext:combinePath($tmpdir, 'manifest.xml'))" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>"</xsl:text>
      <xsl:if test="$p">
        <xsl:text> /arg:</xsl:text>
        <xsl:value-of select="$p" />
      </xsl:if>
      <xsl:if test="$output">
        <xsl:text> /out:"</xsl:text>
        <xsl:variable name="path">
          <xsl:apply-templates select="." mode="path-from-root">
            <xsl:with-param name="root" select="ancestor::manifest[1]" />
          </xsl:apply-templates>
        </xsl:variable>
        <xsl:value-of select="nyext:combinePath(nyext:combinePath($outdir, $path), $output)" />
        <xsl:text>"</xsl:text>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$args" />
    <xsl:value-of select="nyext:startTransform($trmdir, $args)" />
  </xsl:template>

  <xsl:template match="file">
    <xsl:variable name="path">
      <xsl:apply-templates select="." mode="path-from-root">
        <xsl:with-param name="root" select="ancestor::solution[1]" />
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="output">
      <xsl:choose>
        <xsl:when test="@type='res'">
          <xsl:value-of select="concat(@name, '.resx')" />
        </xsl:when>
        <xsl:when test="@type">
          <xsl:value-of select="concat(@name, '.', @type)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(@name, '.cs')" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:apply-templates select="transformation" mode="generation">
      <xsl:with-param name="params">
        <xsl:text>name=</xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>,outdir=</xsl:text>
        <xsl:value-of select="nyext:getShortPathName(nyext:combinePath($outdir, $path))" />
      </xsl:with-param>
      <xsl:with-param name="output" select="$output" />
    </xsl:apply-templates>
    <xsl:apply-templates select="execution" mode="generation">
      <xsl:with-param name="output" select="$output" />
    </xsl:apply-templates>
	  <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="project">
    <xsl:apply-templates select="transformation" mode="generation">
      <xsl:with-param name="params">
        <xsl:text>id=</xsl:text>
        <xsl:value-of select="@id" />
        <xsl:text>,trmdir=</xsl:text>
        <xsl:value-of select="nyext:getShortPathName($trmdir)" />
      </xsl:with-param>
      <xsl:with-param name="output">
        <xsl:choose>
          <xsl:when test="@type='sql'">
            <xsl:value-of select="concat(@name, '.dbp')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat(@name, '.csproj')" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:apply-templates>
	  <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="solution">
    <xsl:apply-templates select="transformation" mode="generation">
      <xsl:with-param name="params">
        <xsl:text>name=</xsl:text>
        <xsl:value-of select="@name" />
      </xsl:with-param>
      <xsl:with-param name="output" select="concat(@name, '.sln')" />
    </xsl:apply-templates>
	  <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="/">
	  <xsl:apply-templates />
  </xsl:template>

</xsl:stylesheet>